﻿// -----------------------------------------------------------------------
// <copyright file="ConfigData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Config
    /// </summary>
    public class ConfigData
    {
        /// <summary>
        /// Get Config By Id
        /// </summary>
        /// <param name="configId">Param configId</param>
        /// <returns>Return Config</returns>
        public ConfigEN GetConfigById(int configId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ConfigEN config = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigGetById, configId);
                while (reader.Read())
                {
                    config = new ConfigEN();

                    if (!reader.IsDBNull(0))
                    {
                        config.ConfigId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        config.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        config.Default = reader.GetBoolean(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        config.ServerName = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return config;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Config
        /// </summary>
        /// <returns>Return list all Config</returns>
        public IList<ConfigEN> GetConfigs()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ConfigEN> lstConfig = new List<ConfigEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigGetAll);
                while (reader.Read())
                {
                    ConfigEN config = new ConfigEN();

                    if (!reader.IsDBNull(0))
                    {
                        config.ConfigId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        config.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        config.Default = reader.GetBoolean(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        config.ServerName = reader.GetString(3);
                    }

                    lstConfig.Add(config);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstConfig;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Config
        /// </summary>
        /// <param name="config">Paarm Config</param>
        /// <returns>Insert Config</returns>
        public int Insert(ConfigEN config)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Config.Code, config.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Config.Default, config.Default),
                                    new SqlParameter(Constants.SqlParamerterName.Config.ServerName, config.ServerName)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Config
        /// </summary>
        /// <param name="config">Param Config</param>
        public void Update(ConfigEN config)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Config.ConfigId, config.ConfigId),
                                    new SqlParameter(Constants.SqlParamerterName.Config.Code, config.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Config.Default, config.Default),
                                    new SqlParameter(Constants.SqlParamerterName.Config.ServerName, config.ServerName)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Config by configId
        /// </summary>
        /// <param name="configId">param configId</param>
        public void Delete(int configId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigDelete, configId);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="configId">Param configId</param>
        /// <param name="code">Param code</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int configId, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Config.ConfigId, configId),
                    new SqlParameter(Constants.SqlParamerterName.Config.Code, code)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConfigExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}